# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=ponyc
pkgver=0.43.1
pkgrel=1
pkgdesc='An actor model, capabilities, high performance programming language'
url='https://ponylang.org/'
arch=('loong64' 'x86_64')
license=('BSD')
depends=('llvm-libs')  # 'libb2'
makedepends=('benchmark' 'cmake' 'gtest' 'llvm')
checkdepends=('pcre2')
options=('!strip')
optdepends=('pcre2: Needed for the regex package')
source=("https://github.com/ponylang/ponyc/archive/$pkgver/$pkgname-$pkgver.tar.gz"
        shared-libs.patch)
sha512sums=('7558e40c6d370b1ead57ce66408f1a4d9359720c7d85aee9d4d4f089fc9591e7bcbd12bf4d34e0a1640a71a39afd81cd965c46ddc2a90943d385865df41f1eb6'
            '7dc4591d5e3ff7d321f4397f382d700e3778b984df3d1dc95ace1689ce3f7fe63d2b35c4986aea11cf15cfa34f53b96782eebe2cc8a729f47de45f72302ea594')

prepare() {
  cd ponyc-$pkgver

  patch -p2 -i ../shared-libs.patch

  # Use system gmock/gtest/gbenchmark, remove -Werror (https://github.com/ponylang/ponyc/issues/3806)
  sed -i 's|NO_DEFAULT_PATH||;s/-Werror //' CMakeLists.txt

  # Don't build libponyc-standalone.a
  # sed -i 's/else()/elseif(FALSE)/' src/libponyc/CMakeLists.txt

  # System b2 - disabled: *** stack smashing detected ***: terminated
  # sed -i 's/blake2/b2/' src/ponyc/CMakeLists.txt test/libponyc/CMakeLists.txt benchmark/libponyc/CMakeLists.txt
  # Arguments ordering changed
  # sed -i 's|blake2b(buf, SIGNATURE_LENGTH, array.ptr, array.size, NULL, 0);|blake2b(buf, array.ptr, NULL, SIGNATURE_LENGTH, array.size, 0);|' src/libponyc/pkg/package.c

  # Missing LLVM components
  sed -i 's/demangle/demangle amdgpucodegen avrcodegen bpfcodegen codegen hexagoncodegen lanaicodegen mipscodegen msp430codegen nativecodegen nvptxcodegen powerpccodegen riscvcodegen sparccodegen systemzcodegen webassemblycodegen xcorecodegen amdgpuasmparser asmparser avrasmparser bpfasmparser hexagonasmparser lanaiasmparser mipsasmparser msp430asmparser powerpcasmparser riscvasmparser sparcasmparser systemzasmparser webassemblyasmparser/' CMakeLists.txt
}

build() {
  cd ponyc-$pkgver
  make config=release prefix=/usr arch=x86-64 libs
  make config=release prefix=/usr arch=x86-64 configure
  make config=release prefix=/usr arch=x86-64 build

  mkdir docs
  build/release/ponyc packages/stdlib -rexpr -g -o docs
}

check() {
  cd ponyc-$pkgver
  make config=release prefix=/usr arch=x86-64 test
}

package() {
  cd ponyc-$pkgver
  make config=release arch= DESTDIR="$pkgdir"/usr/lib/pony install

  install -dm755 "$pkgdir"/usr/{bin,lib,include/pony/detail}
  ln -sf /usr/lib/pony/bin/ponyc "$pkgdir"/usr/bin/
  for _lib in {libponyc,libponyrt,libponyrt-pic}.a; do
    ln -sf /usr/lib/pony/lib/$_lib "$pkgdir"/usr/lib/
  done
  ln -sf /usr/lib/pony/include/pony.h "$pkgdir"/usr/include/
  ln -sf /usr/lib/pony/include/pony/detail/atomics.h "$pkgdir"/usr/include/pony/detail/

  # Let's include the examples as documentation is scarce.
  install -dm755 "$pkgdir"/usr/share/doc/pony
  cp -dr --no-preserve=ownership examples "$pkgdir"/usr/share/doc/pony

  cp -dr docs/* "$pkgdir"/usr/share/doc/pony

  install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/"$pkgname"/LICENSE
}
